#ifndef __ESBUFFER_H
#define __ESBUFFER_H


//es_buffer的缓存长度
#ifndef ES_BUFFER_SIZE 		
#define ES_BUFFER_SIZE 128
#endif



#include <stdint.h>
#include <string.h>

/**
 *  缓存类
 *  例：
 *  es_Buffer <char> buf;
 *
 *  int main()
 *  {
 *      buf = 'a';
 *      buf.put('b');
 *      char *head = buf.head();
 *      puts(head);
 *
 *      char whats_in_there[2] = {0};
 *      int pos = 0;
 *
 *      while(buf.available())      //重点用法
 *      {
 *          whats_in_there[pos++] = buf;
 *      }
 *      printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
 *      buf.clear();
 *      error("done\n\n\n");
 *  }
 */

template <typename T>
class es_Buffer
{
private:
	T   _buf[ES_BUFFER_SIZE];		//缓存数组指针
	volatile uint32_t   _wloc;		//写指针
	volatile uint32_t   _rloc;		//读指针
	int _dataSize;		//已接收数据长度

public:
	//初始化,指定缓存长度（循环使用的缓存）
	es_Buffer();

	//获取已缓存数据长度
	uint32_t getSize();


	~es_Buffer();

	//存入数据到缓存结尾
	void put(T data);

	//从缓存开头获取数据并删除
	T get(void);

	//获取顶指针
	T *head(void);

	//清空缓存并重置所有指针
	void clear(void);

	//有数据时返回1
	uint32_t available(void);

	//运算符重载
	es_Buffer &operator= (T data)
	{
		put(data);
		return *this;
	}

	//操作符重载
	operator int(void)
	{
		return get();
	}

	//获取顶元素，但不移除
	T peek();

};

#endif

